package com.java.day4Collection;

import java.util.Iterator;

public class MyClass implements Iterator<String>,Iterable<String> {
    private String[] stuNames = {"张三","李四","王五","赵六","周七","刘八","陈九"};

    //当前下标
    private int index = -1;

    @Override
    public boolean hasNext() {
        return index < stuNames.length - 1;
    }

    @Override
    public String next() {
        //向后移动下标
        index++;
        //返回当前数据
        return stuNames[index];
    }

    @Override
    public Iterator<String> iterator() {
        return this;
    }

    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        //for循环会自动调用iterator()方法，再执行迭代器的hasNext和Next方法
        for (String name : myClass) {
            System.out.println(name);
        }

        //循环判断是否还有数据
//        while(myClass.hasNext()){
//            //向后移动，并获得数据
//            String s = myClass.next();
//            System.out.println(s);
//        }
    }


}
